<?php
/**
 * The Crux of our User Code section
 *
 *
 **/

require_once('../util/'.$GLOBALS['__NAV_UTIL']['math'].'/math.php');

function smurfize( $string, $validator ) {
// XXX I just don't like this. It seems too much of a strech for the language how it's set up.
// Must be a less complex way. Maybe hand write?
  function getTag( $matches ) {
    return "[url: \"". (($matches[1]) ? $matches[1] : $matches[2]) ."\"]${matches[2]}[/url:]";
  }
  $count = 1;

  // transpose all [X ""] tags
  $count = 1;
  while( $count ) {
    $string = preg_replace_callback( '/(?:\[url\]|\[url "(.*?)"\])(.*?)\[\/url\]/', 'getTag', $string, -1, $count );
  }
  // add validators to all tags
  $count = 1;
  while( $count ) {
    //$string = preg_replace('/\[([a-zA-Z]*)\](.*)\[\/\1\](?!.*\[\/\1\])/', " [$1:${validator}]$2[/\$1:${validator}] ", $string, -1, $count );
// XXX Need to capture the url info and paste it in as a resul \\1 or something
    $string = preg_replace('/\[(([a-zA-Z]*)[:]?)( "(?:.*?)")?](.*?)\[\/\\1\]/', " [$2:${validator}$3]:${validator}$4[/\$2:${validator}] ", $string, -1, $count ); 
  }
 
  return $string;
}
function smurfclean( $string, $validator ) {

  $string = stripslashes(str_replace( ":${validator}", '', $string )); 
  return $string;
}

function desmurf( $string, $valid ) {

  $string = nl2br(stripslashes($string));

  $codes = array ( "[url:$valid \"", "\"]:$valid", "[/url:$valid]",
                   "[style:$valid \"", "[/style:$valid]",
                   "[b:$valid]:$valid", "[/b:$valid]",
                   "[i:$valid]:$valid", "[/i:$valid]",
                   "[u:$valid]:$valid", "[/u:$valid]",
                   "[img:$valid]:$valid", "[/img:$valid]",
                   "[code:$valid]:$valid", "[/code:$valid]",
                   "[quote:$valid]:$valid", "[/quote:$valid]",
                   ":p"
                  );
  $htmls = array ( '<a href="', '">', '</a>',
                   '<span class="post.', '</span>',
                   '<strong class="post">', '</strong>',
                   '<em class="post">', '</em>',
                   '<u class="post">', '</u>',
                   '<img class="post" src="', '" />',
                   '<pre class="code post">', '</pre>',
                   '<p class="post quote" >', '</p>',
                   '<img src="smile.png" />'
                 );
  $string= str_replace( $codes, $htmls, $string ); 

  return $string;
}

?>
